package cn.kgc.web;

import cn.kgc.entity.Department;
import cn.kgc.entity.Employee;
import cn.kgc.mapper.DepartmentMapper;
import cn.kgc.service.DepartmentService;
import cn.kgc.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/doLogin")
    public String doLogin(@RequestParam(name = "name") String userName, @RequestParam(name = "password") String userPwd, RedirectAttributes model, HttpSession session){
        Employee employee = employeeService.login(userName, userPwd);
        if (employee == null){
            model.addFlashAttribute("error","<font size='5'>用户名或密码错误！</font>");
            return "redirect:/";
        }else{
            session.setAttribute("loginEmp",employee);
            return "redirect:index";
        }
    }

/*    @RequestMapping("/index")
    public String index(){
        return "index";
    }*/

    /*@RequestMapping("/showList")*/
    @GetMapping("/emps")
    public String showEmpList(Model model,
                              @RequestParam(name="pageId",defaultValue = "1") int pageId,
                              @RequestParam(name = "pageSize",defaultValue = "3") int pageSize){
        PageInfo<Employee> pageInfo = employeeService.findEmpList(pageId,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "emp/list";
    }

    @GetMapping("/emp")
    public String toAddEmp(Model model){
        List<Department> deptList = departmentService.findAll();
        model.addAttribute("deptList",deptList);
        return "emp/edit";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeService.addEmployee(employee);
        return "redirect:emps";
    }

    @GetMapping("/emp/{empId}")
    public  String toModify(@PathVariable("empId") Integer id,Model model){
        Employee employeeId = employeeService.findEmployeeId(id);
        List<Department> all = departmentService.findAll();

        model.addAttribute("employee",employeeId);
        model.addAttribute("deptList",all);

        return  "emp/edit";
    }

    @PutMapping("/emp")
    public String modify(Employee employee){
       employeeService.modifyEmployee(employee);
        return "redirect:emps";
    }

    @DeleteMapping("/emp/{empId}")
    @ResponseBody
    public Map<String,Boolean> remove(@PathVariable("empId") Integer id){
        Map<String,Boolean> result=new HashMap<>();
        Boolean success=employeeService.removeEmployee(id);
        result.put("success",success);
        return  result;
    }

}
